﻿using AutoMapper;
using BS.Domain.ProcessTable;
using BS.Infra;
using BS.Infra.Interface;
using MediatR;

namespace BS.Write.Api.Handler.Process
{
    public class InsertProcessingComandHandler : IRequestHandler<InsertProcessingCompanyRequestDTO, ApiResult>
    {
        private readonly IProcessingRepository _processingRepository;

        private readonly IMapper _mapper;

        private readonly ILogger<InsertProcessingComandHandler> _logger;

        public InsertProcessingComandHandler(IProcessingRepository processingRepository, IMapper mapper, ILogger<InsertProcessingComandHandler> logger)
        {
            _processingRepository = processingRepository;
            _mapper = mapper;
            _logger = logger;
        }
        public async Task<ApiResult> Handle(InsertProcessingCompanyRequestDTO request, CancellationToken cancellationToken)
        {
            try
            {
                var process = _mapper.Map<Processing>(request);
                var pone= await _processingRepository.InsertAsync(process);
                return ApiResult.Ok(pone);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Insert processing company failed.");
                return  ApiResult.Fail( ex.Message);
            }
        }
    }
}
public class InsertProcessingCompanyRequestDTO : IRequest<ApiResult>
{
    /// <summary>
    /// 企业名称
    /// </summary>
    public string EnterpriseName { get; set; } = string.Empty;
    /// <summary>
    /// 生产许可证号
    /// </summary>
    public string ProductionLicense { get; set; } = string.Empty;
}